﻿using System;
using System.Threading;

namespace TestTimerConsole
{
    class Program
    {
        public const int max_count = 3;
        private static int count = 0;
        public static Timer _timer = null;

        static void Main(string[] args)
        {
            var dueTime = Calc_Timer_dueTime();
            _timer = new Timer(b => CallBackFunc(), null, dueTime, Timeout.InfiniteTimeSpan);
            Console.ReadKey();
        }

        private static void CallBackFunc()
        {
            DateTime nowTime = DateTime.Now;
            Console.WriteLine($"当前时间: {nowTime:yyyy-MM-dd HH:mm:ss}, 否秒为零: {0 == nowTime.Second}");
            count++;
            if (count >= max_count)
            {
                _timer.Dispose();
                Console.WriteLine($"定时器结束执行");
                return;
            }
            var dueTime = Calc_Timer_dueTime();
            //var v = Timeout.InfiniteTimeSpan;
            //_timer.Change((long)period.TotalMilliseconds, 0);
            _timer.Change(dueTime, Timeout.InfiniteTimeSpan);
        }

        private static TimeSpan Calc_Timer_dueTime()
        {
            DateTime nt = DateTime.Now;
            var tt = new DateTime(nt.Year, nt.Month, nt.Day, nt.Hour, nt.Minute, 0, 0, nt.Kind).AddMinutes(1);
            var span = tt - nt;
            Console.WriteLine($"返回总毫秒数: {span.TotalMilliseconds}");
            return span;
            //return (long)span.TotalMilliseconds;
        }
    }
}
